using System;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using Framework;
using LuaInterface;


    [RequireComponent(typeof(TextMeshProUGUI))]
    public class TMPHyperLinkHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    {
        private static Camera _uiCamera;
        private static Camera _PCamera
        {
            get
            {
                if (null == _uiCamera)
                {
                    _uiCamera = GameObject.Find("UIRoot").GetComponent<Canvas>().worldCamera;
                }
                return _uiCamera;
            }
        }

        private TextMeshProUGUI _meshText;
        private TextMeshProUGUI _PMeshText
        {
            get
            {
                if (null == _meshText)
                {
                    _meshText = GetComponent<TextMeshProUGUI>();
                }
                return _meshText;
            }
        }

        private Vector3 _lastPressScreenPosition;
        private bool _needCalculateHover;
        private GeneralCallBack _onHover;
        private GeneralCallBack _onDesHover;

        public void SetHoverListener(LuaFunction cb, LuaTable cbObj)
        {
            GeneralCallBack.GetOrReUse(ref _onHover, cb, cbObj);
        }
        
        public void SetDesHoverListener(LuaFunction cb, LuaTable cbObj)
        {
            GeneralCallBack.GetOrReUse(ref _onDesHover, cb, cbObj);
        }
        
        [NoToLua]
        public void OnPointerEnter(PointerEventData eventData)
        {
            if (null == _onHover)
            {
                return;
            }

            _needCalculateHover = true;
        }

        [NoToLua]
        public void OnPointerExit(PointerEventData eventData)
        {
            _needCalculateHover = false;
        }
        
        [NoToLua]
        private void CalculateHoverPosition()
        {
            Vector3 screenPosition;
            if (FrameworkSettings.CurPlatform == FrameworkSettings.WindowsPlayer)
            {
                screenPosition = Input.mousePosition;
            }
            else
            {
                screenPosition = Input.touches[0].position;
            }

            if (screenPosition == _lastPressScreenPosition)
            {
                return;
            }

            _lastPressScreenPosition = screenPosition;
            int linkIdx = TMP_TextUtilities.FindIntersectingLink(_PMeshText, screenPosition, _PCamera);
            if (-1 != linkIdx)
            {
                TMP_LinkInfo linkInfo = _PMeshText.textInfo.linkInfo[linkIdx];
                if (null != _onHover)
                {
                    GeneralCallBack.Invoke(ref _onHover, linkInfo.GetLinkID(), screenPosition);
                }
            }
            else
            {
                if (null != _onDesHover)
                {
                    GeneralCallBack.Invoke(ref _onDesHover);
                }
            }
        }

        [NoToLua]
        private void Update()
        {
            if (_needCalculateHover)
            {
                CalculateHoverPosition();
            }
        }

        [NoToLua]
        private void OnDestroy()
        {
            GeneralCallBack.RecycleInst(ref _onHover);
            GeneralCallBack.RecycleInst(ref _onDesHover);
        }
        
    }


